package com.lili.collection.restful.plugins;

import com.lili.collection.core.context.BatchRecordContext;
import com.lili.collection.core.context.StageContext;
import com.lili.collection.core.context.impl.GenericBatchRecordContext;
import com.lili.collection.core.error.InputRunnerException;
import com.lili.collection.core.error.StageRecordException;
import com.lili.collection.core.plugins.InputPlugin;
import com.lili.collection.core.runner.result.ErrorDetail;
import com.lili.collection.core.runner.result.RecordInfo;
import com.lili.collection.core.runner.result.RunnerResult;
import com.lili.collection.core.utils.HandlerUtil;
import com.lili.collection.core.utils.StrCutUtil;
import com.lili.collection.restful.config.DataInputConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 *描述: http接口数据输入插件
 *@date: 2023/10/13  11:28
 *@author: lili
 */
@Slf4j
public class DataInputPlugin implements InputPlugin<DataInputConfig> {

    /**
     * 别名映射
     */
    private Map<String,String> aliasMap;

    /**
     * 运行结果
     */
    private RunnerResult runnerResult;

    /**
     * 输入配置
     */
    private DataInputConfig inputConfig;

    /**
     * 系统异常
     */
    private Throwable throwable;

    @Override
    public void setRunnerResult(RunnerResult result) {
        this.runnerResult=result;
    }

    @Override
    public StageContext execute() {
        BatchRecordContext recordContext = new GenericBatchRecordContext();
        RecordInfo recordInfo=new RecordInfo();
        try {
            //原始格式发送
            recordInfo = HandlerUtil.getRecordInfo(inputConfig, inputConfig.getData(), recordContext);
        } catch (Throwable e) {
            this.throwable=e;
            recordInfo.setSuccessed(false);
            ErrorDetail detail=new ErrorDetail();
            detail.setPropType("DIRECT-DATA");
            detail.setPropValue(StrCutUtil.getCutLengthData(inputConfig.getData()));
            recordInfo.getErrOutputData().addErrorValueDetail(detail);
            recordContext.setStageRecordState(StageContext.StageRecordState.ALL_ERROR);
            recordContext.setException(new StageRecordException(e));
            log.error("接口数据获取错误!",e);
        }
        if (this.throwable==null) {
            recordContext.setStageRecordState(StageContext.StageRecordState.ALL_SUCCESS);
            recordContext.setStageContextState(StageContext.StageContextState.COMPLETE);
            if(recordInfo!=null){
                this.runnerResult.setSuccessRecordCount(recordInfo.getSuccessRecordCount());
                this.runnerResult.setTotalRecordCount(recordInfo.getTotalRecordCount());
                this.runnerResult.setErrorRecordCount(recordInfo.getErrorRecordCount());
            }
            log.info("-->数据全部读取完成-->总数据：{}，成功数：{}",
                    this.runnerResult.getTotalRecordCount(),
                    this.runnerResult.getSuccessRecordCount());
        }else {
            if(recordInfo!=null){
                recordInfo.setSuccessed(false);
                this.runnerResult.setErrOutputData(recordInfo.getErrOutputData());
            }
        }
        return recordContext;
    }

    @Override
    public void init(DataInputConfig inputConfig) {
        try {
            this.inputConfig=inputConfig;
            if(this.inputConfig==null){
                throw new IllegalArgumentException("接口采集输入配置为空!");
            }
            if(StringUtils.isBlank(this.inputConfig.getData())){
                throw new IllegalArgumentException("处理数据不能为空!");
            }
            this.aliasMap=this.inputConfig.getAliasMap();
            if(MapUtils.isEmpty(this.aliasMap)){
                throw new IllegalArgumentException("接口列配置映射字段为空!");
            }
        } catch (Throwable e) {
            log.error("接口采集初始化错误！",e);
            throw new InputRunnerException(e);
        }
    }

    @Override
    public void dispose() {
    }
}